<?php
class RefinancingSystemsController extends AppController {
    var $name = 'RefinancingSystems';

    function index() {
        $this->RefinancingSystem->recursive = 0;
        $this->set('refinancingSystems', $this->paginate());
    }

    function view($id = null) {
        if (!$id) {
            $this->Session->setFlash(__('Invalid refinancing system', true));
            $this->redirect(array('action' => 'index'));
        }
        $this->set('refinancingSystem', $this->RefinancingSystem->read(null, $id));
    }

    function add() {
        if (!empty($this->data)) {
            $this->RefinancingSystem->create();
            if ($this->RefinancingSystem->save($this->data)) {
                $this->Session->setFlash(__('The refinancing system has been saved', true));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The refinancing system could not be saved. Please, try again.', true));
            }
        }
        $buildings = $this->RefinancingSystem->Building->find('list');
        $amortizationSystems = $this->RefinancingSystem->AmortizationSystem->find('list',
                array('conditions' => array('AmortizationSystem.lookup_type' => 'AMORTIZATION_SYSTEM')));
        $controller = $this;
        $this->set(compact('buildings', 'amortizationSystems'), 'controller' );
    }

    function edit($id = null) {
        if (!$id && empty($this->data)) {
            $this->Session->setFlash(__('Invalid refinancing system', true));
            $this->redirect(array('action' => 'index'));
        }
        if (!empty($this->data)) {
            if ($this->RefinancingSystem->save($this->data)) {
                $this->Session->setFlash(__('The refinancing system has been saved', true));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The refinancing system could not be saved. Please, try again.', true));
            }
        }
        if (empty($this->data)) {
            $this->data = $this->RefinancingSystem->read(null, $id);
        }
        $buildings = $this->RefinancingSystem->Building->find('list');
        $amortizationSystems = $this->RefinancingSystem->AmortizationSystem->find('list',
                array('conditions' => array('AmortizationSystem.lookup_type' => 'AMORTIZATION_SYSTEM')));
        $this->set(compact('buildings', 'amortizationSystems'));
    }
}
?>